package ua.khpi.si.kit49.diploma.db.dto;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import ua.khpi.si.kit49.diploma.core.Weather;

/**
 * Class represents city
 * 
 * @author netstalk3r
 * 
 */

@Entity
@Table(name = "city")
public class City {

	@Id
	@Access(AccessType.PROPERTY)
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	private Integer id;

	@Column(name = "woeid", unique = true)
	private Integer woeid;

	@Column(name = "name", unique = true, length = 45)
	private String name;

	@Transient
	private Weather weather;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "cityStart", cascade = CascadeType.ALL)
	private Set<Connection> connectionsStart = new HashSet<Connection>();

	@OneToMany(mappedBy = "cityFinish", cascade = CascadeType.ALL)
	@Fetch(FetchMode.JOIN)
	private Set<Connection> connectionsFinish = new HashSet<Connection>();

	public City() {
		super();
		this.weather = new Weather();
	}

	public City(Integer woeid, String name, Weather weather,
			Set<Connection> connectionsStart) {
		super();
		this.woeid = woeid;
		this.name = name;
		this.weather = weather;
		this.connectionsStart = connectionsStart;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getWoeid() {
		return woeid;
	}

	public void setWoeid(Integer woeid) {
		this.woeid = woeid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Weather getWeather() {
		return weather;
	}

	public void setWeather(Weather weather) {
		this.weather = weather;
	}

	public Set<Connection> getConnectionsStart() {
		return connectionsStart;
	}

	public void setConnectionsStart(Set<Connection> connectionsStart) {
		this.connectionsStart = connectionsStart;
	}

	public Set<Connection> getConnectionsFinish() {
		return connectionsFinish;
	}

	public void setConnectionsFinish(Set<Connection> connectionsFinish) {
		this.connectionsFinish = connectionsFinish;
	}

	@Override
	public String toString() {
		return "City: name - " + name + "; id - " + id + "; woeid - " + woeid;
	}

	@Override
	public int hashCode() {
		int hash = 31;
		hash = hash * 17 + ((id == null) ? 0 : id.hashCode());
		hash = hash * 17 + ((woeid == null) ? 0 : woeid.hashCode());
		hash = hash * 17 + ((name == null) ? 0 : name.hashCode());
		hash = hash * 17 + ((weather == null) ? 0 : weather.hashCode());
		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof City))
			return false;
		City entry = (City) obj;
		return id.equals(entry.getId()) && woeid.equals(entry.getWoeid())
				&& name.equals(entry.getName())
				&& weather.equals(entry.getWeather());
	}

}